package com.example.wearing.index;

import java.util.ArrayList;

public class MatchRecommend {
    /*
    设计推荐机制：
    1、衣物数量很少——提示”只有达到一定数量和种类，才可以进行推荐“
    2、衣物数量足够
        -计算最佳舒适温度Ts，需要纬度和月份
        -计算体感温度Tg，需要温度、湿度、风速
        -计算为某地温度与真实温度的偏差值，Dt=22.7-Ts
        -由 Tg 和 Dt 的值查表， 可以得出人体感觉、舒适度以及穿衣等级和适宜服装
        -当用户连续两天反馈冷/热，将等级进行+/-1的调整
     */

    private double latitude;//纬度
    private int month; //月份
    private double Ta; //当前温度
    private double RH, RHs; //相对湿度%，最适相对湿度%
    private double V; //风速，m/s

    //计算每个人的 “最佳舒适温度” Ts,Ts=22.7×[1.0-0.3sin(φ-23.5)]-0.3cos[15×(M-1)]
    public static double getTs(double latitude, int month){
        double Ts = 0.0;
        double a = Math.toRadians(latitude - 23.5);//把数字转换成度
        double a1 = 1.0 - 0.3*Math.sin(a);
        double b = Math.toRadians(15*(month-1));
        double b1 = 0.3 * Math.cos(b);
        Ts = 22.7 * a1 - b1;
        return Ts;
    }

    //使用公式中，有些许问题，将14*去掉了
    //计算体感温度Tg，Tg=Ta+14C1{exp[C2(Ta-Ts)(RH-RHs)]+C3}+C4(Ta-Ts)V
    public static double getTg(double Ta, double Ts, double RH, double V, boolean isRain){
        //c1,c2,c3,c4为常数
        double C1, C2, C3, C4, Tg = 0.0;
        if(Ta >= Ts){
            C1 = 1;
            C2 = 0.05;
            C3 = -1;
            C4 = -0.03;
        }else{
            C1 = -1;
            C2 = -0.013;
            C3 = 1;
            C4 = 0.01;
        }
        double RHs;
        if(isRain){
            //有降水
            RHs = 61.8;
        }else
            RHs = 50.0;

        double a = C2*(Ta-Ts)*(RH-RHs);
        double a1 = Math.exp(a)+C3;
        double b = C4*V*(Ta-Ts);
        System.out.println("Rh="+RH+",a="+a+",a1="+a1+",b="+b);
        Tg = Ta+C1*a1+b;
        return Tg;
    }

    //获取穿衣等级
    public static int getWeaLevel(double latitude, int month, double Ta, double RH, double V, boolean isRain){
        double Ts = MatchRecommend.getTs(latitude, month);
        System.out.println("最佳舒适温度Ts="+Ts);
        double Tg = MatchRecommend.getTg(Ta, Ts, RH, V, isRain);
        System.out.println("体感温度Tg="+Tg);
        double Dt = 22.7 - Ts;
        if((32-Dt) < Tg){
            return 4;
        }else if((29-Dt)<Tg && Tg<=(32-Dt)){
            return 3;
        }else if((25-Dt)<Tg && Tg<=(29-Dt)){
            return 2;
        }else if((23-Dt)<Tg && Tg<=(25-Dt)){
            return 1;
        }else if((18-Dt)<Tg && Tg<=(23-Dt)){
            return 0;
        }else if((13-Dt)<Tg && Tg<=(18-Dt)){
            return -1;
        }else if((6-Dt)<Tg && Tg<=(13-Dt)){
            return -2;
        }else if((-2-Dt)<Tg && Tg<=(6-Dt)){
            return -3;
        }else if((-10-Dt)<Tg && Tg<=(-2-Dt)){
            return -4;
        }else if((-20-Dt)<Tg && Tg<=(-10-Dt)){
            return -5;
        }else{
            return -6;
        }
    }

    //根据穿衣等级，获取穿衣推荐
    public static ArrayList<String[]> getMatch(int level){
        //"短袖", "长袖", "卫衣", "衬衫", "毛衣", "外套", "大衣", "棉服", "羽绒服"
        //"牛仔裤", "运动裤", "打底裤", "裙子", "短裤", "西装裤"
        //"皮鞋", "运动鞋", "帆布鞋", "高跟鞋", "靴子", "棉鞋"
        //"帽子", "围巾", "项链", "墨镜", "手表", "耳饰"
        //确定四个分类后，在数据库中搜索名称对应衣服，进行搭配
        ArrayList<String[]> clothesOfMatch = new ArrayList<>();
        String[] clothes1 = {};
        String[] clothes2 = {};
        String[] clothes3 = {};
        String[] clothes4 = {};

        if(level==2 || level==3 || level==4){
            //2、3、4级为热、炎热、酷热，穿衣为宽松散热，短袖、短裤、短裙等
            clothes1 = new String[]{"短袖"};
            clothes2 = new String[]{"短裤", "裙子"};
            clothes3 = new String[]{"运动鞋", "帆布鞋", "高跟鞋"};
            clothes4 = new String[]{"项链", "墨镜", "手表", "耳饰"};

        }else if(level==0 || level==1){
            //0、1级为舒适、微热，较舒适，穿衣为易散热，T恤、衬衫、卫衣、长裤等
            clothes1 = new String[]{"长袖", "卫衣", "衬衫"};
            clothes2 = new String[]{"牛仔裤", "运动裤", "西装裤"};
            clothes3 = new String[]{"皮鞋", "运动鞋", "帆布鞋", "高跟鞋"};
            clothes4 = new String[]{"项链", "手表", "耳饰"};

        }else if(level == -1){
            //-1级为凉爽，穿衣为薄衣服，针织衫、马甲、线衣、西服套装等
            clothes1 = new String[]{"长袖", "卫衣", "衬衫"};
            clothes2 = new String[]{"毛衣", "外套"};
            clothes3 = new String[]{"牛仔裤", "运动裤", "西装裤"};
            clothes4 = new String[]{"皮鞋", "运动鞋", "帆布鞋", "高跟鞋", "靴子"};

        }else if(level==-2 || level==-3){
            //-2、-3级为凉、冷，穿衣为中等厚度，风衣、大衣、羊绒衫、毛衣、加绒卫衣等
            clothes1 = new String[]{"毛衣", "长袖", "卫衣", "衬衫"};
            clothes2 = new String[]{"外套", "大衣"};
            clothes3 = new String[]{"牛仔裤", "运动裤", "西装裤"};
            clothes4 = new String[]{"皮鞋", "运动鞋", "帆布鞋", "高跟鞋", "靴子"};

        }else if(level==-4 || level==-5 || level==-6){
            //-4、-5、-6级为很冷、寒冷、严寒，穿衣为较厚的保暖衣物，棉衣、羽绒服、羊绒衫、毛衣等
            clothes1 = new String[]{"毛衣", "长袖", "卫衣", "衬衫"};
            clothes2 = new String[]{"大衣", "棉服", "羽绒服"};
            clothes3 = new String[]{"牛仔裤", "运动裤", "西装裤"};
            clothes4 = new String[]{"皮鞋", "运动鞋", "帆布鞋", "高跟鞋", "靴子", "棉鞋"};
        }
        clothesOfMatch.add(clothes1);
        clothesOfMatch.add(clothes2);
        clothesOfMatch.add(clothes3);
        clothesOfMatch.add(clothes4);

        return clothesOfMatch;
    }
}
